layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
      elem: '#startTime'
    });

    laydate.render({
      elem: '#endTime'
    });

    /**
     * 成品包装管理
     */
    var FruitsPacking = {
        tableId: "fruitsPackingTable"
    };

    /**
     * 初始化表格的列
     */
    FruitsPacking.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'states', sort: true, title: '状态', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'times', sort: true, title: '时间', style:'font-weight: 600;'},
            {field: 'userNumber', sort: true, title: '包装人编码', style:'font-weight: 600;'},
            {field: 'userName', sort: true, title: '包装人姓名', style:'font-weight: 600;'},
            {field: 'goodsId', sort: true, title: '商品id', hide: true},
            {field: 'goodsName', sort: true, title: '商品名称', style:'font-weight: 600;'},
            {field: 'grades', sort: true, title: '等级', style:'font-weight: 600;'},
            {field: 'caseWeightOne', sort: true, title: '每箱重量', style:'font-weight: 600;'},
            {field: 'caseSpecs', sort: true, title: '箱规', style:'font-weight: 600;', hide: true},
            {field: 'caseWeight', sort: true, title: '箱皮', style:'font-weight: 600;', totalRowText: '合计：'},
            {field: 'gweight', sort: true, title: '毛重', style:'font-weight: 600;', totalRow: true},
            {field: 'nweight', sort: true, title: '净重', style:'font-weight: 600;', totalRow: true},
            {field: 'nums', sort: true, title: '数量', style:'font-weight: 600;', totalRow: true},
            {field: 'producetime', sort: true, title: '生产日期', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: '库位', style:'font-weight: 600;'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FruitsPacking.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['grades'] = $("#grades").val();
        queryData['caseWeightOne'] = $("#caseWeightOne").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        table.reload(FruitsPacking.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    FruitsPacking.exportExcel = function () {
        var checkRows = table.checkStatus(FruitsPacking.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FruitsPacking.tableId,
        url: Feng.ctxPath + '/fruitsPacking/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: FruitsPacking.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FruitsPacking.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FruitsPacking.exportExcel();
    });

    // 单击行事件
    table.on('row(' + FruitsPacking.tableId + ')', function (obj) {
		$('div[lay-id="' + FruitsPacking.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + FruitsPacking.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
});
